<?php
// yc@200-10-19
class DbMysql implements Db{

    private $dbh = null;

    public function __construct()
    {
        $this->connect();
    }
    public function connect()
    {
        global $ENV;
        $this->dbh = mysql_connect($ENV['db.host'], $ENV['db.user'], $ENV['db.pass']);
        if (!$this->dbh || !mysql_select_db($ENV['db.name'], $this->dbh)){
            debug('Db::connect() failed.');
            debug();
        }
        debug('db: connection established.');
        //http://www.livid.cn/doc_view.php?doc_id=4177
        $this->query('SET NAMES `utf8`');
        $this->query('SET CHARACTER SET utf8');
        $this->query('SET COLLATION_CONNECTION=\'utf8_general_ci\'');
    }

    public function close()
    {
        if ($this->dbh)
            mysql_close($this->dbh);
    }

    public function count($table, $where)
    {
        return $this->fetchResult($table, 'count(1)', $where);
    }

    public function query($sql)
    {
        debug("db: <strong>{$sql}</strong>");
        return mysql_query($sql, $this->dbh);
    }
    
    public function insert($table, $data = array())
    {
        $fields = clearSlashes(array_keys($data));
        quoteData($data);
        $sql = "INSERT INTO {$table}(" . implode(',', $fields) . ') VALUES (' . implode(',', $data) . ')';
        return $this->query($sql);
    }

    public function lastInsertId()
    {
        return mysql_insert_id($this->dbh);
    }

    public public function update($table, $data = array(), $where = '1=0')
    {
        $fields = clearSlashes(array_keys($data));
        quoteData($data);
        foreach ($data as $k => $v)
            $data[$k] = "{$k}={$v}";
        $sql = "UPDATE {$table} SET " . implode(',', $data) . " WHERE {$where}";
        return $this->query($sql);
    }
    //@return bool
    public function delete($table, $where = '1=0')
    {
        $sql = "DELETE FROM {$table} WHERE {$where}";
        return $this->query($sql);
    }
    //@return string||number
    public function fetchResult($table, $what, $where = '1=1')
    {
        $sql = "SELECT {$what} FROM {$table} WHERE $where LIMIT 1";
        $res = $this->query($sql);
        return mysql_result($res, 0, 0);
    }
    //@return Array
    public function fetchRow($table, $where)
    {
        $sql = "SELECT * FROM {$table} WHERE {$where} LIMIT 1";
        $res = $this->query($sql);
        return mysql_fetch_assoc($res);
    }
    //@return Array of Array
    public function fetchAll($sql)
    {
        return $this->fetchAssoc($sql);    
    }
    //@return Array of Array
    public function fetchAssoc($sql)
    {
        $res = $this->query($sql);
        $ret = array();
        while (($row = mysql_fetch_assoc($res)) !== FALSE)
            $ret[] = $row;
        return $ret;
    }
    //@return Array of Array
    public function fetchArray($sql)
    {
        $res = $this->query($sql);
        $ret = array();
        while (($row = mysql_fetch_array($res)) !== FALSE)
            $ret[] = $row;
        return $ret;
    }
}
?>
